import subprocess
import time

def pppd():
    cmd = ["bash", "/etc/ppp/peers/quectel-pppd.sh"]

    process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)

    while True:
        # 读取标准输出流
        output = process.stdout.readline()
        if output == '' and process.poll() is not None:
            break
        if output:
            line_context = output.strip()
            print(output.strip())

            if line_context.startswith("Script /etc/ppp/ip-up started"):

                print("------------4G启动成功------------")

                pppd_service_cmd = ["systemctl", "restart", "auto-ssh"]

                subprocess.Popen(pppd_service_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)


    # 检查命令是否成功执行
    returncode = process.poll()
    if returncode == 0:
        time.sleep(4)
        pppd()
    else:
        error_message = process.stderr.read()
        print(f"Command execution failed: {error_message}")

if __name__ == "__main__":

    pppd()